package com.ruoyi.web.controller.system.factory;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.RoleEnum;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.core.JoController;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class SysUserFactory extends JoController
{
    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysPostService postService;

    public List<SysUser> listData(SysUser sysUser)
    {
        List<SysUser> listData = Lists.newArrayList();
        // if (SecurityUtils.hasRole(RoleEnum.ADMIN.getCode()))
        // {
        //     listData = userService.list(Wrappers.lambdaQuery(SysUser.class)
        //             .select(SysUser::getUserId, SysUser::getUserName, SysUser::getDeptId
        //                     , SysUser::getAvatar, SysUser::getNickName, SysUser::getPhonenumber));
        // } else
        // {
        //     if (SecurityUtils.hasRole(RoleEnum.MANAGER.getCode()) ||
        //         SecurityUtils.hasRole(RoleEnum.GROUPLEADER.getCode()))
        //     {
        //         sysUser.setDeptId(getDeptId());
        //         listData = userService.selectUserList(sysUser);
        //     } else if (SecurityUtils.hasRole(RoleEnum.COMMON.getCode()))
        //     {
        //         listData = userService.list(Wrappers.lambdaQuery(SysUser.class).eq(SysUser::getUserId, getUserId())
        //                 .select(SysUser::getUserId, SysUser::getUserName, SysUser::getDeptId
        //                         , SysUser::getAvatar, SysUser::getNickName, SysUser::getPhonenumber));
        //     } else
        //     {
        //         listData = userService.selectUserList(sysUser);
        //     }
        // }
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId()))
        {
            listData = userService.list(Wrappers.lambdaQuery(SysUser.class)
                    .select(SysUser::getUserId, SysUser::getUserName, SysUser::getDeptId
                            , SysUser::getAvatar, SysUser::getNickName, SysUser::getPhonenumber));
        } else
        {
            listData = userService.selectUserList(sysUser);
        }
        return listData;
    }

    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
    {
        userService.checkUserDataScope(userId);
        AjaxResult ajax = AjaxResult.success();
        List<SysRole> roles = roleService.selectRoleAll();
        ajax.put("roles", SysUser.isAdmin(userId) ? roles :
                roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        ajax.put("posts", postService.selectPostAll());
        if (StringUtils.isNotNull(userId))
        {
            SysUser sysUser = userService.selectUserById(userId);
            ajax.put(AjaxResult.DATA_TAG, sysUser);
            ajax.put("postIds", postService.selectPostListByUserId(userId));
            ajax.put("roleIds", sysUser.getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList()));
        }
        return ajax;
    }

    public SysUser getSysUser(Long userId)
    {
        return userService.selectUserById(userId);
    }

}
